﻿using System.IO;
using System.Linq;
using PluginFramework.Packages;
using Xunit;
using System;
using System.Diagnostics;

namespace PluginTests.PackageTests
{
	public class ScanPackagesTests
	{
		private Stopwatch _t = new Stopwatch();
		private readonly PackageScan _scan;

		public ScanPackagesTests()
		{
			_scan = new PackageScan(new DirectoryInfo(Setup.PluginDir));
		}

		[Fact]
		public void load_two_manifests()
		{
			var manifests = _scan.GetAvailableManifests();
			Assert.Equal(2,manifests.Count());
		}

		[Fact]
		public void manifest_is_loaded_correctly()
		{
			var manifests = _scan.GetAvailableManifests();
			var second = manifests.First(d => d.Name == "Plugin2");
			Assert.Equal(second.FriendlyName , "Plugin 2");
			Assert.Equal(second.Description , "Test Plugin 2");
			Assert.Equal(second.Author , "John Smith");
			Assert.True(string.IsNullOrEmpty(second.WebsiteUrl));
		}

		[Fact]
		public void no_manifests_returns_empty_list()
		{
			var scan = new PackageScan(new DirectoryInfo(@"..\..\Cache"));
			var manifests = scan.GetAvailableManifests();
			Assert.NotNull(manifests);
			Assert.Equal(0,manifests.Count());
		}

		[Fact]
		public void two_Plugin_info_returned()
		{
			var infos = _scan.EnumeratePackageAssemblies(new[] {"Plugin1", "Plugin2"});
			Assert.Equal(2,infos.Count());
		}

		[Fact]
		public void missing_Plugins_are_ignored()
		{
			var infos = _scan.EnumeratePackageAssemblies(new[] { "Plugin1", "Plugin" });
			Assert.Equal(1, infos.Count());
			Assert.Equal("Plugin1",infos.First().Name);
		}

		[Fact]
		public void null_list_returns_empty_Plugins()
		{
			var infos = _scan.EnumeratePackageAssemblies(null);
			Assert.NotNull(infos);
			Assert.Equal(0,infos.Count());
		}
		private void Write(string format, params object[] param)
		{
			Console.WriteLine(format, param);
		}
	}
}